package com.liuheng.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuheng.manager.service.ProductService;
import com.liuheng.spzx.model.dto.product.ProductDto;
import com.liuheng.spzx.model.entity.product.Product;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product/product")
public class ProductController {
 @Autowired
 private ProductService productService;
 @GetMapping("/{page}/{limit}")
 public Result<Page<Product>> pageProduct(@PathVariable int page, @PathVariable int limit, ProductDto productDto){
  Page pageInfo=productService.findPage(page,limit,productDto);
  return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
 }
 @PostMapping("/save")
 public Result saveProduct(@RequestBody Product product){
  productService.saveProduct(product);
  return Result.build(null,ResultCodeEnum.SUCCESS);
 }
 @GetMapping("/getById/{id}")
 public Result<Product> getById(@PathVariable Long id){
  Product product=productService.getProductById(id);
  return Result.build(product,ResultCodeEnum.SUCCESS);
 }
 @PutMapping("/updateById")
 public Result updateById(@RequestBody Product product){
  productService.updateByProductId(product);
  return Result.build(null,ResultCodeEnum.SUCCESS);
 }
 @DeleteMapping("/deleteById/{id}")
 public Result deleteByProductId(@PathVariable Long id){
  productService.deleteByProductId(id);
  return Result.build(null,ResultCodeEnum.SUCCESS);
 }
 @GetMapping("/updateStatus/{id}/{status}")
 public Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
  Product product = new Product();
  product.setId(id);
  if(status == 1) {
   product.setStatus(1);
  } else {
   product.setStatus(-1);
  }
  productService.updateById(product);
  return Result.build(null,ResultCodeEnum.SUCCESS);
 }
 @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
 public Result updateAuditStatus(@PathVariable Long id,@PathVariable Integer auditStatus){
  Product product = new Product();
  product.setId(id);
  if(auditStatus == 1) {
   product.setAuditStatus(1);
   product.setAuditMessage("审批通过");
  } else {
   product.setAuditStatus(-1);
   product.setAuditMessage("审批不通过");
  }
  productService.updateById(product);
  return  Result.build(null,ResultCodeEnum.SUCCESS);
 }
}
